package ch07;

import java.util.ArrayList;

class _19_PolyArgumentTest3 {
	public static void main(String args[]) {
		Buyer19 b = new Buyer19();
		Tv17 tv = new Tv17();
		Computer com = new Computer();
		Audio audio = new Audio();

		b.buy(tv);
		b.buy(com);
		b.buy(audio);
		b.summary();
		System.out.println();
		b.refund(com);
		b.summary();
	}
}

class Buyer19 { 
	int money = 1000;
	int bonusPoint = 0;
	ArrayList<Product> item = new ArrayList<Product>();

	void buy(Product p) {
		if (money < p.price) {
			System.out.println("money is scarce.");
			return;
		}
		money -= p.price;
		bonusPoint += p.bonusPoint;
		item.add(p);
		System.out.println("buy " + p);
	}

	void refund(Product p) {
		if (item.remove(p)) {
			money += p.price;
			bonusPoint -= p.bonusPoint;
			System.out.println("refund " +p);
		} else	System.out.println("no item refunded.");
	}

	void summary() {
		int sum = 0;
		String itemList ="";
		
		if(item.isEmpty()) {
			System.out.println("no item purchased.");
			return;
		}
		
		for(int i=0; i<item.size();i++) {
			Product p = item.get(i);	
			sum += p.price;
			itemList += p + " ";
		}
		
		System.out.println("purchase money: " + sum);
		System.out.println("item purchased: " + itemList);
	}
}
/*
buy Tv
buy Computer
buy Audio
purchase money: 350
item purchased: Tv Computer Audio 

refund Computer
purchase money: 150
item purchased: Tv Audio 
*/
